package com.example.jobback.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.security.SignatureException;
import java.util.Date;
import java.util.Map;

/**
 * @ClassName: JWTUtil
 * @Description: jwt身份验证工具类
 * @Author: lhb
 * @Date: 2025/5/10
 */

public class JWTUtil {

    public static String generateToken(String userIdentity,Long time,String secret){
        return Jwts.builder()
                .setSubject(userIdentity)
                .setExpiration(new Date(System.currentTimeMillis()+ time))
                .signWith(SignatureAlgorithm.HS256, secret.getBytes())
                .compact();
    }

    public static Claims parseToken(String token, String secret) throws Exception{
        String tokenValue=token.substring(7);
        return Jwts.parser()
                .setSigningKey(secret.getBytes())
                .parseClaimsJws(tokenValue)
                .getBody();
    }

    public static String checkToken(String token, HttpServletResponse response, String secret) throws IOException {
        Map<String,Object> claims;
        try{
            claims= JWTUtil.parseToken(token,secret);
        }
        catch (SignatureException e){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write("签名验证失败");
            return null;
        }
        catch (ExpiredJwtException e){
            response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            response.getWriter().write("token时效过期1111");
            return null;
        }
        catch (IllegalArgumentException e){
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getWriter().write("客户端发送参数有误");
            return null;
        }
        catch (Exception e){
            response.setStatus(HttpServletResponse.SC_CONFLICT);
            response.getWriter().write("验证失败");
            return null;
        }

        return claims.get("sub").toString();
    }

    public static String checkToken(String token, String secret){
        Map<String,Object> claims;
        try{
            claims= JWTUtil.parseToken(token,secret);
        }
        catch (Exception e){
            return null;
        }
        return claims.get("sub").toString();
    }




//    public static void main(String[] args) {
//        String phone="18020681996";
//        String encrypt=null;
//        try {
//            encrypt = SecureEncryptionUtil.encrypt(phone, "wx6ea1b3d58ba5825e", "de0bea9331af57df9b12decad68318d4");
//            System.out.println("Encrypt: " + encrypt);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        String secret="woshiniba";
//        Long jwtTime=14400000L;
//        String token=generateToken(encrypt,jwtTime,secret);
//        System.out.println("getToken:"+token);
//        try {
//            Claims claims = parseToken(token, secret);
//            System.out.println("sub:"+claims.get("sub").toString());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

}
